﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ECCSystem.BizRule;
using ECC_EXT_DAO.Dao;
using ECC_EXT_Entity;

namespace ECCSystem
{
    public partial class frm_Ext_OFFCategory : Form
    {
        public frm_Ext_OFFCategory()
        {
            InitializeComponent();
        }

        private void frm_EXT_OFFCategory_Load(object sender, EventArgs e)
        {
           this.LoadDatagridView();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            this.LoadDatagridView();
        }

        private void LoadDatagridView()
        {
            OFFCategoryEntitySearch _search = new OFFCategoryEntitySearch();
            //_search.RecordDateFrom = DateTime.Parse(this.dtp_RecordDate_From.Value.ToShortDateString());
            _search.RecordDateFrom = this.dtp_RecordDate_From.Value.Date;
            
            //_search.RecordDateTo = DateTime.Parse(this.dtp_RecordDate_To.Value.ToShortDateString());
            _search.RecordDateTo = this.dtp_RecordDate_To.Value.Date;

            if ("".Equals(this.textBox_RecoedUser.Text))
                _search.RecordUser = null;
            else
                _search.RecordUser = this.textBox_RecoedUser.Text;
            _listRes = new OFFCategoryDao().Search(_search);
            this.dataGridView1.DataSource =
                this.ConvertDto(_listRes);
        }

        private IList<OFFCategoryEntity> _listRes; 

        private IList<OFFCategoryGridViewDto> ConvertDto(IList<OFFCategoryEntity> _listSrc)
        {
            IList<OFFCategoryGridViewDto> _list = new List<OFFCategoryGridViewDto>();
            string CaseNo = "";
            string AccountNo = "";


            foreach (var item in _listSrc)
            {
                _list.Add(new OFFCategoryGridViewDto(item));
            }
            return _list;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if(e.ColumnIndex == 2 && e.RowIndex != -1)
            {
                int vID =
                    int.Parse(this.dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString());

                foreach (var item in _listRes)
                {
                    if(vID == item.Id)
                    {
                        this.Hide();
                        if(DialogResult.OK ==
                            new frm_Ext_OFFCategoryItems(item.OFFCategoryItems).ShowDialog())
                        {
                            item.FollowUser = ClsCommon.gStrOperatorName;
                            item.FollowDate = DateTime.Now;

                            new OFFCategoryDao().SaveOrUpdate(item);
                            this.LoadDatagridView();
                        }
                        this.Show();
                        break;
                    }

                }
                this.dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();

                //String str =
                //    this.dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
                //Clipboard.SetText(str);

                //if(DialogResult.OK ==
                //    MessageBox.Show(str + "in clipboard,confirm input!", "Confirm", MessageBoxButtons.OKCancel))
                //{
                //    OFFCategoryDao _offCategoryDao = new OFFCategoryDao();
                //    OFFCategoryEntity _entity =
                //        _offCategoryDao.GetById(this.dataGridView1[0, e.RowIndex].Value.ToString());
                //    _entity.FollowUser = ClsCommon.gStrOperatorName;
                //    _entity.FollowDate = DateTime.Now;

                //    _offCategoryDao.SaveOrUpdate(_entity);
                //    this.LoadDatagridView();
                //}
            }
        }

    }

    internal class OFFCategoryGridViewDto
    {
        public OFFCategoryGridViewDto(OFFCategoryEntity _entity)
        {
            Id = _entity.Id;
            RecordUser = _entity.RecordUser;
            RecordDate = _entity.RecordDate;
            CaseNo = _entity.CaseNo;
            AccountNo = _entity.AccountNo;
            //CategoryName = _entity.DICCategory.Name;
            //CategoryCode = _entity.DICCategory.Code;
            FollowUser = _entity.FollowUser;
            FollowDate = _entity.FollowDate.HasValue?_entity.FollowDate.Value.ToString() : "";
        }

        public long Id { get; set; }

        public string RecordUser { get; set; }

        public DateTime RecordDate { get; set; }

        public string CaseNo { get; set; }

        public string AccountNo { get; set; }

        //public string CategoryName { get; set; }

        //public string CategoryCode { get; set; }

        public String FollowUser { get; set; }

        public String FollowDate { get; set; }
    }
}
